//
//  TKYSegmentView.m
//  StationHouseHelper
//
//  Created by apple on 2018/6/14.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYSegmentView.h"

@interface TKYSegmentView()
@property (nonatomic, strong)UIButton *stationHouseButton;
@property (nonatomic, strong)UILabel *stationHouseLine;

@property (nonatomic, strong)UIButton *routeButton;
@property (nonatomic, strong)UILabel *routeLine;

@property (nonatomic, strong)UIView *gray_view;


@property (nonatomic, strong)UIView *botton_line;

@end
@implementation TKYSegmentView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
       
        [self addSubview:self.stationHouseButton];
        [self addSubview:self.stationHouseLine];
        //[self addSubview:self.routeButton];
        [self addSubview:self.routeLine];
        [self stationHouseButtonClick:self.stationHouseButton];
        [self addSubview:self.gray_view];
        [self addSubview:self.botton_line];
        [self addSubview:self.stationHouseLine];

    }
    return self;
}

- (UIButton *)stationHouseButton{
    if (_stationHouseButton == nil) {
        _stationHouseButton = [UIButton buttonWithType: UIButtonTypeCustom];
        _stationHouseButton.frame = CGRectMake(kWidthScale *20 , self.gray_view.kx_bottom, kWidthScale *40, kHeightScale*35);
        [_stationHouseButton setTitle:@"站房" forState:UIControlStateNormal];
        [_stationHouseButton setTitleColor:kRGBColor(45, 135, 255, 1) forState:
         UIControlStateSelected];
        _stationHouseButton.titleLabel.font = Font(14);
        [_stationHouseButton setTitleColor:[UIColor grayColor] forState:
         UIControlStateNormal];
        [_stationHouseButton addTarget:self action:@selector(stationHouseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _stationHouseButton;
}

- (UILabel *)stationHouseLine{
    if (_stationHouseLine == nil) {
        _stationHouseLine = [[UILabel alloc]init];
        _stationHouseLine.backgroundColor = kRGBColor(45, 135, 255, 1) ;
    }
    return _stationHouseLine;
}

- (UIButton *)routeButton{
    if (_routeButton == nil) {
        _routeButton = [UIButton buttonWithType: UIButtonTypeCustom];
        _routeButton.frame = CGRectMake(_stationHouseButton.kx_right + kWidthScale *20 , self.gray_view.kx_bottom, kWidthScale *40, kHeightScale*35);
        [_routeButton setTitle:@"路线" forState:UIControlStateNormal];
        [_routeButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

        [_routeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        //[_routeButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

        [_routeButton addTarget:self action:@selector(routeButtoClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _routeButton;
}

- (UILabel *)routeLine{
    if (_routeLine == nil) {
        _routeLine = [[UILabel alloc]init];
        // 立刻根据文字内容计算label的宽度
        //        _stationHouseLine.kx_height = 1;
        //        _stationHouseLine.kx_y = self.kx_height - self.kx_height;
        //        [_stationHouseButton.titleLabel sizeToFit];
        _routeLine.backgroundColor = [UIColor whiteColor];
        // _stationHouseLine.kx_width = _stationHouseButton.titleLabel.kx_width;
        // _stationHouseLine.kx_centerX = _stationHouseButton.kx_centerX;
    }
    return _routeLine;
}

- (UIView *)botton_line{
    if (_botton_line == nil) {
        _botton_line = [[UIView alloc]initWithFrame:CGRectMake(0, self.kx_height - 1, kScreenWidth *0.8, 1)];
        _botton_line.backgroundColor = kMainColour;
    }
    return _botton_line;
}

- (UIView *)gray_view{
    if (_gray_view == nil) {
        _gray_view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth *0.8, 15)];
        _gray_view.backgroundColor = kMainColour;
    }
    return _gray_view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _stationHouseLine.kx_height = 1;
    _stationHouseLine.kx_y = self.kx_height - 1;
    [_stationHouseButton.titleLabel sizeToFit];
     _stationHouseLine.kx_width = _stationHouseButton.titleLabel.kx_width;
     _stationHouseLine.kx_centerX = _stationHouseButton.kx_centerX;
    
}

- (void)routeButtoClick:(UIButton *)button{
    button.selected = YES;
    self.stationHouseButton.selected = NO;
    self.stationHouseButton.titleLabel.textColor = [UIColor grayColor];
    self.stationHouseLine.backgroundColor = [UIColor grayColor];
    self.routeLine.backgroundColor = [UIColor redColor];
    self.routeButton.titleLabel.textColor = [UIColor redColor];
    //self.routeButton.backgroundColor = [UIColor redColor];
    [UIView animateWithDuration:0.25 animations:^{
        self.stationHouseLine.hidden = YES;
        self.routeLine.hidden = NO;
    } completion:nil];
    
    _routeLine.kx_height = 1;
    _routeLine.kx_y = self.kx_height - 1;
    [_routeButton.titleLabel sizeToFit];
    _routeLine.kx_width = _routeButton.titleLabel.kx_width;
    _routeLine.kx_centerX = _routeButton.kx_centerX;
    
    if (self.stationHouseButtonBlock) {
        self.stationHouseButtonBlock(button);
    }
}

- (void)stationHouseButtonClick:(UIButton *)button{
    button.selected = YES;
    self.stationHouseButton.titleLabel.textColor = kRGBColor(45, 135, 255, 1);

    self.stationHouseLine.backgroundColor = kRGBColor(45, 135, 255, 1);
    self.routeLine.backgroundColor = [UIColor grayColor];
    self.routeButton.titleLabel.textColor = [UIColor grayColor];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.stationHouseLine.hidden = NO;
        self.routeLine.hidden = YES;
    } completion:nil];
    if (self.routeButtonBlock) {
        self.routeButtonBlock(button);
    }
}
@end
